import os
from PySide6.QtWidgets import QWidget, QVBoxLayout, QLabel, QFrame
from PySide6.QtCore import Qt
from PySide6.QtGui import QPixmap

class ValvePanel(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setup_ui()
        
    def setup_ui(self):
        valve_outer_layout = QVBoxLayout(self)
        valve_outer_layout.setAlignment(Qt.AlignCenter)  # 居中对齐
        
        # 创建一个固定大小的容器框架
        self.valve_container = QFrame()
        self.valve_container.setFixedSize(180, 180)  # 固定大小为180x180
        self.valve_container.setFrameShape(QFrame.StyledPanel)
        self.valve_container.setStyleSheet("background-color: #f0f0f0; border: 1px solid #cccccc;")
        
        # 将蝶阀容器添加到外部容器中
        valve_outer_layout.addWidget(self.valve_container)
        
        # 蝶阀区域使用相对布局
        valve_layout = QVBoxLayout(self.valve_container)
        valve_layout.setContentsMargins(5, 5, 5, 5)
        valve_layout.setSpacing(0)  # 移除内部间距
        
        # 创建蝶阀图片标签 - 用于显示图片
        self.valve_image_label = QLabel(self.valve_container)
        self.valve_image_label.setAlignment(Qt.AlignCenter)
        self.valve_image_label.setFixedSize(170, 170)  # 也固定大小，留出边距
        self.valve_image_label.setStyleSheet("border: none; background-color: transparent;")
        valve_layout.addWidget(self.valve_image_label)
        
        # 创建状态文本标签
        self.valve_status_label = QLabel("开", self.valve_container)
        self.valve_status_label.setAlignment(Qt.AlignCenter)
        self.valve_status_label.setStyleSheet(
            "font-size: 16px; font-weight: bold; color: white; "
            "background-color: rgba(0, 128, 0, 180); border-radius: 10px; padding: 2px 8px;"
        )
    
    def load_valve_image(self, image_name):
        """加载蝶阀图片并显示"""
        image_path = os.path.join("images", image_name)
        if os.path.exists(image_path):
            pixmap = QPixmap(image_path)
            if not pixmap.isNull():
                # 缩放图片以适应标签，保持固定大小
                scaled_pixmap = pixmap.scaled(
                    170, 170,  # 固定大小，比容器小10像素，留出边距
                    Qt.KeepAspectRatio,
                    Qt.SmoothTransformation
                )
                
                # 显示图片
                self.valve_image_label.setPixmap(scaled_pixmap)
                
                # 获取状态并更新标签
                status = "开" if "开" in image_name else "关" if "关" in image_name else "常态"
                self.valve_status_label.setText(status)
                
                # 根据状态设置不同颜色
                if status == "开":
                    self.valve_status_label.setStyleSheet(
                        "font-size: 16px; font-weight: bold; color: white; "
                        "background-color: rgba(0, 128, 0, 180); border-radius: 10px; padding: 2px 8px;"
                    )
                elif status == "关":
                    self.valve_status_label.setStyleSheet(
                        "font-size: 16px; font-weight: bold; color: white; "
                        "background-color: rgba(128, 0, 0, 180); border-radius: 10px; padding: 2px 8px;"
                    )
                else:
                    self.valve_status_label.setStyleSheet(
                        "font-size: 16px; font-weight: bold; color: white; "
                        "background-color: rgba(0, 0, 128, 180); border-radius: 10px; padding: 2px 8px;"
                    )
                
                # 更新标签位置 - 放在图片底部中央
                self.valve_status_label.adjustSize()
                self.valve_status_label.move(
                    (self.valve_container.width() - self.valve_status_label.width()) // 2,
                    self.valve_container.height() - self.valve_status_label.height() - 10
                )
                self.valve_status_label.show()
            else:
                self.valve_image_label.setText(f"无法加载图片")
                self.valve_status_label.hide()
        else:
            self.valve_image_label.setText(f"未找到图片")
            self.valve_status_label.hide()
